package com.jiangfeixiang.springbootblog.service.impl;

import com.jiangfeixiang.springbootblog.dao.UsersMapper;
import com.jiangfeixiang.springbootblog.entity.Users;
import com.jiangfeixiang.springbootblog.entity.UsersExample;
import com.jiangfeixiang.springbootblog.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: 姜飞祥
 * @Description:
 * @Date: Create in 2019/1/20/0020 14:23
 * @param: $params$
 * @return: $returns$
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UsersServiceImpl implements UsersService {

    @Autowired
    private UsersMapper usersMapper;

    /**
     * 管理员登录，根据用户名密码登录
     * @param users
     * @return
     */
    @Override
    public Users login(Users users) {
        Users user = usersMapper.selectByUser(users);
        return user;
    }

    /**
     * 校验用户名是否存在
     * @param users
     * @return
     */
    @Override
    public int checkUsername(Users users) {
        UsersExample usersExample = new UsersExample();
        UsersExample.Criteria criteria = usersExample.createCriteria();
        criteria.andUsernameEqualTo(users.getUsername());
        int i = usersMapper.countByExample(usersExample);
        return i;
    }

    /**
     * 校验邮箱是否存在
     * @param users
     * @return
     */
    @Override
    public int checkEmail(Users users) {
        UsersExample usersExample = new UsersExample();
        UsersExample.Criteria criteria = usersExample.createCriteria();
        criteria.andEmailEqualTo(users.getEmail());
        int i = usersMapper.countByExample(usersExample);
        return i;
    }

    /**
     * 用户注册
     * @param users
     */
    @Override
    public int register(Users users) {
        return usersMapper.insertSelective(users);
    }


    /**
     * 根据Id查询用户信息
     * @param users
     * @return
     */
    @Override
    public Users getUserInfo(Users users) {

        Users user = usersMapper.getUserInfo(users);
        return users;
    }
}
